<?php

class MenuItemController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }


    public function accessRules() {
        return array();
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }


    public function actionCreate() {
        $model = new MenuItem;

        if (isset($_POST['MenuItem'])) {
            $model->attributes = $_POST['MenuItem'];
            $model->position = $_POST['MenuItem']['position'];
            if ($model->save()) {
                $session['update_catalog_show'] = true;
                Yii::app()->session->add('update_catalog_show', $session);
                $this->redirect(array('index'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['MenuItem'])) {
            $model->attributes = $_POST['MenuItem'];
            $model->position = $_POST['MenuItem']['position'];
            if ($model->save()) {
                $session['update_catalog_show'] = true;
                Yii::app()->session->add('update_catalog_show', $session);
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();
        $session['update_catalog_show'] = true;
        Yii::app()->session->add('update_catalog_show', $session);
        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new MenuItem('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['MenuItem']))
            $model->attributes = $_GET['MenuItem'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = MenuItem::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
